//费用报销流程
function setValueToDetail() {
    var baseInfo=WfForm.getBaseInfo();//返回当前请求基础信息
    var nodeid=baseInfo.nodeid;//获取当前节点id
    //费用科目
    var fykmFieldid = WfForm.convertFieldNameToId("fykm", "detail_5");
    //费用科目编码
    var fykmbmFieldid = WfForm.convertFieldNameToId("fykmbm", "detail_5");
    //预算科目
    var yskmFieldid = WfForm.convertFieldNameToId("yskm", "detail_5");

    //预算科目编码
    var yskmbmFieldid = WfForm.convertFieldNameToId("yskmbia", "detail_5");
    //部门/成本中心
    var cbzxFieldid = WfForm.convertFieldNameToId("bmcbzx", "detail_5");
    //部门/成本中心编码
    var cbzxbmFieldid = WfForm.convertFieldNameToId("bmcbzxbm", "detail_5");
    //报销/分摊金额
    var amountFieldid = WfForm.convertFieldNameToId("bxftjebhhkcz", "detail_5");

    // var dt1Count=WfForm.getDetailRowCount("detail_5");
    var result=true;
    //1.将明细表数据放入数组中
    var arry=[];
    var isEmpty=false;
    var strRows=WfForm.getDetailAllRowIndexStr("detail_5");
    var rowsArray1=strRows.split(",");
    for (var i=0;i<rowsArray1.length;i++){
        var rowIndex=parseInt(rowsArray1[i]);
        var fykmVal=WfForm.getFieldValue(fykmFieldid + "_" + rowIndex);
        var fykmbmVal = WfForm.getFieldValue(fykmbmFieldid + "_" + rowIndex);
        var yskmVal = WfForm.getFieldValue(yskmFieldid + "_" + rowIndex);
        var yskmName= WfForm.getBrowserShowName(yskmFieldid + "_" + rowIndex);
        var yskmbmVal = WfForm.getFieldValue(yskmbmFieldid + "_" + rowIndex);
        var cbzxVal = WfForm.getFieldValue(cbzxFieldid + "_" + rowIndex);
        var bmName= WfForm.getBrowserShowName(cbzxFieldid + "_" + rowIndex);
        var cbzxbmVal = WfForm.getFieldValue(cbzxbmFieldid + "_" + rowIndex);
        var amountVal = WfForm.getFieldValue(amountFieldid + "_" + rowIndex);
        var groupCol=yskmVal+cbzxVal;
        if(fykmVal!="") {
            var obj = new Object();
            obj.yskm = yskmVal;
            obj.yskmName = yskmName;
            obj.yskmbm = yskmbmVal;
            obj.cbzx = cbzxVal;
            obj.bmName = bmName;
            obj.cbzxbm = cbzxbmVal;
            obj.groupCol = groupCol;
            obj.amount = amountVal;
            arry.push(obj);
        }else{
            isEmpty=true;
            break;
        }
    }
    if(!isEmpty) {
        //2.对数组分组统计,放入新数组
        var map = {},
            dest = [];
        for (var i = 0; i < arry.length; i++) {
            var ai = arry[i];
            if (map[ai.groupCol]==undefined) {
                dest.push({
                    yskm:ai.yskm,
                    yskmName:ai.yskmName,
                    yskmbm:ai.yskmbm,
                    cbzx:ai.cbzx,
                    bmName:ai.bmName,
                    cbzxbm:ai.cbzxbm,
                    groupCol:ai.groupCol,
                    amount:ai.amount
                });
                map[ai.groupCol]=ai;
            } else {
                //将相同分组的金额进行累计
                map[ai.groupCol].amount = parseFloat(Number(map[ai.groupCol].amount) + Number(ai.amount)).toFixed(2);
            }
        }
        // alert("Map："+JSON.stringify(map));
        if(JSON.stringify(map)!="{}") {//map对象中有值才进行下一步操作
            //3.对要插入数据的明细表进行数据筛选及数据操作
            //预算科目
            var yskmFieldid6 = WfForm.convertFieldNameToId("yskm", "detail_6");
            var yskmName6 = WfForm.convertFieldNameToId("yskmName", "detail_6");
            //预算科目编码
            var yskmbmFieldid6 = WfForm.convertFieldNameToId("yskmbm", "detail_6");
            //部门/成本中心
            var bmFieldid = WfForm.convertFieldNameToId("bm", "detail_6");
            var bmName6 =  WfForm.convertFieldNameToId("bmName", "detail_6");
            //部门/成本中心编码
            var bmbmFieldid = WfForm.convertFieldNameToId("bmbm", "detail_6");
            //报销金额
            var bxjeFieldid = WfForm.convertFieldNameToId("bxje", "detail_6");
            var dt6Count = WfForm.getDetailRowCount("detail_6");
            var array6 = [];
            var rowindex=0;
            var str=WfForm.getDetailAllRowIndexStr("detail_6");
            var rowsArray=str.split(",");
            // alert("开始行遍历数组："+rowsArray.toString());
            for (var i =0; i < rowsArray.length; i++) {
                var index=parseInt(rowsArray[i]);
                var yskmVal = WfForm.getFieldValue(yskmFieldid6 + "_" + index);
                var yskmName = WfForm.getFieldValue(yskmName6 + "_" + index);
                var yskmbmVal = WfForm.getFieldValue(yskmbmFieldid6 + "_" + index);
                var bmVal = WfForm.getFieldValue(bmFieldid + "_" + index);
                var bmName = WfForm.getFieldValue(bmName6 + "_" + index);
                var cbzxbmVal = WfForm.getFieldValue(bmbmFieldid + "_" + index);
                var bxVal=WfForm.getFieldValue(bxjeFieldid + "_" + index);
                var groupCol = yskmVal + bmVal;
                if (groupCol != "") {
                    array6.push(groupCol);
                    //集合中不存在,则删除该行
                    if (map[groupCol]==undefined) {
                        //todo
                        WfForm.delDetailRow("detail_6", index.toString());
                    } else {//更新该行
                        WfForm.changeFieldValue(bxjeFieldid + "_" + index, {value: map[groupCol].amount});
                        WfForm.changeFieldValue(yskmName + "_" + index, {value: map[groupCol].yskmName});
                        WfForm.changeFieldValue(bmName + "_" + index, {value: map[groupCol].bmName});
                    }
                }else{
                    WfForm.delDetailRow("detail_6", index.toString());
                }

            }
            // alert("数组元素："+JSON.stringify(array6));
            if (array6.length > 0) {
                for (var obj in map) {
                    // alert("预算科目5="+map[obj].yskm+";部门5="+map[obj].cbzx);
                    var yskmAndbm=map[obj].yskm+map[obj].cbzx;
                    // alert(yskmAndbm);
                    // alert(array6.includes(yskmAndbm));
                   if(array6.includes(yskmAndbm)){//如果为true则更新
                    // alert("更新1");
                   }else{//新增
                       var yskm=map[obj].yskm;//预算科目id
                       var yskmbm=map[obj].yskmbm;//预算科目编码
                       var yskmName=map[obj].yskmName;//预算科目名称
                       var bm=map[obj].cbzx;//部门/成本中心id
                       var bmbm=map[obj].cbzxbm;//部门/成本中心id
                       var bmbm=map[obj].cbzxbm;//部门/成本中心id
                       var bmName=map[obj].bmName;//部门/成本中心名称
                       var amount=map[obj].amount;//报销金额
                       var addObj = {};
                       addObj[yskmFieldid6] = {value:yskm};
                       addObj[yskmbmFieldid6]={value:yskmbm};
                       addObj[yskmName6]={value:yskmName};
                       addObj[bmFieldid]={value:bm};
                       addObj[bmbmFieldid]={value:bmbm};
                       addObj[bmName6]={value:bmName};
                       addObj[bxjeFieldid]={value:amount};
                       WfForm.addDetailRow("detail_6",addObj);
                       // WfForm.addDetailRow("detail_6",{
                       //     [yskmFieldid6]:{value:yskm},
                       //     [yskmbmFieldid6]:{value:yskmbm},
                       //     [yskmName6]:{value:yskmName},
                       //     [bmFieldid]:{value:bm},
                       //     [bmbmFieldid]:{value:bmbm},
                       //     [bmName6]:{value:bmName},
                       //     [bxjeFieldid]:{value:amount}
                       // });
                   }
                }
            } else {//将汇总的明细表数据全部插入到明细表6
                // alert("新增2");
                var rowCount = WfForm.getDetailRowCount("detail_6");
                for (var obj in map) {
                    var yskm=map[obj].yskm;//预算科目id
                    var yskmbm=map[obj].yskmbm;//预算科目编码
                    var yskmName=map[obj].yskmName;//预算科目名称
                    var bm=map[obj].cbzx;//部门/成本中心id
                    var bmbm=map[obj].cbzxbm;//部门/成本中心id
                    var bmName=map[obj].bmName;//部门/成本中心名称
                    var amount=map[obj].amount;//报销金额
                    var addObj = {};
                    addObj[yskmFieldid6] = {value:yskm};
                    addObj[yskmbmFieldid6]={value:yskmbm};
                    addObj[yskmName6]={value:yskmName};
                    addObj[bmFieldid]={value:bm};
                    addObj[bmbmFieldid]={value:bmbm};
                    addObj[bmName6]={value:bmName};
                    addObj[bxjeFieldid]={value:amount};
                    WfForm.addDetailRow("detail_6",addObj);
                    // WfForm.addDetailRow("detail_6",{
                    //     [yskmFieldid6]:{value:yskm},
                    //     [yskmbmFieldid6]:{value:yskmbm},
                    //     [yskmName6]:{value:yskmName},
                    //     [bmFieldid]:{value:bm},
                    //     [bmbmFieldid]:{value:bmbm},
                    //     [bmName6]:{value:bmName},
                    //     [bxjeFieldid]:{value:amount}
                    // });
                }
            }

        }
        // var yskmFieldid6 = WfForm.convertFieldNameToId("yskm", "detail_5");
        // //预算科目编码
        // var yskmbmFieldid6 = WfForm.convertFieldNameToId("yskmbia", "detail_5");
        // var str=WfForm.getDetailAllRowIndexStr("detail_5");
        // var rowsArray=str.split(",");
        // for (var i =0; i < rowsArray.length; i++) {
        //     var index=parseInt(rowsArray[i]);
        //     var yskmVal = WfForm.getFieldValue(yskmFieldid6 + "_" + index);
        //     var yskmbmVal = WfForm.getFieldValue(yskmbmFieldid6 + "_" + index);
        //     alert("预算科目的值"+yskmVal);
        //     var text= WfForm.getBrowserShowName(yskmFieldid6 + "_" + index);
        //     alert("浏览按钮的值"+text);
        // }
    }else{
        result=false;
        alert("请先选择预算科目！");
    }
    if(result){
        alert("数据更新成功！");
    }

}